﻿' BasecampVB - http://www.tidbitsandtangents.com
' Copyright (c) 2009
' by Rusty S. Swayne
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
' INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
' DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
' SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
' SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
' WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
' THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Imports TnT.BasecampVB.DAL

Namespace BLL

    Public Class TimeEntry
        Inherits BasecampBase

        ''' <summary>
        ''' Constructor
        ''' </summary>
        Public Sub New( _
            ByVal time_entry_id As Integer, _
            ByVal person_id As Integer, _
            ByVal todo_item_id As Integer, _
            ByVal project_id As Integer, _
            ByVal entry_date As DateTime, _
            ByVal description As String, _
            ByVal hours As Double)

            Me.time_entry_id = time_entry_id
            Me.person_id = person_id
            Me.todo_item_id = todo_item_id
            Me.project_id = project_id
            Me.entry_date = entry_date
            Me.description = description
            Me.hours = hours

        End Sub

        ''' <summary>
        ''' Returns TimeEntry from Basecamp API
        ''' </summary>
        Public Shared Function GetTimeEntry(ByVal time_entry_id As Integer) As TimeEntry
            Return GetTimeEntryFromTimeEntryDetails(BasecampProvider.API.GetTimeEntry(time_entry_id))
        End Function

        ''' <summary>
        ''' Returns List(Of TimeEntry) for a given project from Basecamp API
        ''' </summary>
        Public Shared Function GetTimeEntriesByProject(ByVal project_id As Integer, Optional ByVal pagenumber As Integer = 0) As List(Of TimeEntry)
            Return GetTimeEntryListFromTimeEntryDetailsCollection(BasecampProvider.API.GetTimeEntriesByProject(project_id, pagenumber))
        End Function

        ''' <summary>
        ''' Returns List(Of TimeEntry) for a given ToDoItem from Basecamp API
        ''' </summary>
        Public Shared Function GetTimeEntriesByToDoItem(ByVal todo_item_id As Integer) As List(Of TimeEntry)
            Return GetTimeEntryListFromTimeEntryDetailsCollection(BasecampProvider.API.GetTimeEntriesByToDoItem(todo_item_id))
        End Function

        ''' <summary>
        ''' Returns List(Of TimeEntryDetails) by report filters from Basecamp API
        ''' </summary>
        Public Shared Function GetTimeEntriesReport(ByVal from_date As DateTime, ByVal to_date As DateTime, Optional ByVal person_id As Integer = 0, Optional ByVal todo_item_id As Integer = 0, Optional ByVal project_id As Integer = 0, Optional ByVal company_id As Integer = 0) As List(Of TimeEntry)
            Return GetTimeEntryListFromTimeEntryDetailsCollection(BasecampProvider.API.GetTimeEntriesReport(from_date, to_date, person_id, todo_item_id, project_id, company_id))
        End Function

        ''' <summary>
        ''' Creates a TimeEntry for a given project in Basecamp via Basecamp API
        ''' </summary>
        Public Shared Function CreateProjectTimeEntry(ByVal project_id As Integer, ByVal person_id As Integer, ByVal entry_date As DateTime, ByVal hours As Double, ByVal description As String) As Integer
            Return BasecampProvider.API.CreateProjectTimeEntry(project_id, person_id, entry_date, hours, description)
        End Function

        ''' <summary>
        ''' Creates a TimeEntry for a given ToDoItem in Basecamp via Basecamp API
        ''' </summary>
        Public Shared Function CreateToDoItemTimeEntry(ByVal todo_item_id As Integer, ByVal person_id As Integer, ByVal entry_date As DateTime, ByVal hours As Double, ByVal description As String) As Integer
            Return BasecampProvider.API.CreateToDoItemTimeEntry(todo_item_id, person_id, entry_date, hours, description)
        End Function

        ''' <summary>
        ''' Updates a TimeEntry in Basecamp via Basecamp API
        ''' </summary>
        Public Overloads Shared Function UpdateTimeEntry(ByVal time_entry_id As Integer, ByVal person_id As Integer, ByVal entry_date As DateTime, ByVal hours As Double, ByVal description As String) As Boolean
            Return UpdateTimeEntry(time_entry_id, person_id, entry_date, hours, description, 0)
        End Function

        ''' <summary>
        ''' Updates a TimeEntry in Basecamp via Basecamp API
        ''' </summary>
        Public Overloads Shared Function UpdateTimeEntry(ByVal time_entry_id As Integer, ByVal person_id As Integer, ByVal entry_date As DateTime, ByVal hours As Double, ByVal description As String, ByVal todo_item_id As Integer) As Boolean
            Return BasecampProvider.API.UpdateTimeEntry(time_entry_id, person_id, entry_date, hours, description, todo_item_id)
        End Function

        ''' <summary>
        ''' Updates a TimeEntry in Basecamp via Basecamp API
        ''' </summary>
        Public Function Update()
            Return UpdateTimeEntry(Me.time_entry_id, Me.person_id, Me.entry_date, Me.hours, Me.description, Me.todo_item_id)
        End Function

        ''' <summary>
        ''' Deletes a TimeEntry in Basecamp via Basecamp API
        ''' </summary>
        Public Shared Function DeleteTimeEntry(ByVal time_entry_id As Integer) As Boolean
            Return BasecampProvider.API.DeleteTimeEntry(time_entry_id)
        End Function

        ''' <summary>
        ''' Deletes a TimeEntry in Basecamp via Basecamp API
        ''' </summary>
        Public Function Delete()
            Dim ret As Boolean = DeleteTimeEntry(Me.time_entry_id)
            Me.time_entry_id = 0
            Return ret
        End Function


#Region "Private Utility Methods"

        ''' <summary>
        ''' Helper Function to create a List(Of TimeEntry) from List(Of TimeEntryDetails) 
        ''' </summary>
        Private Shared Function GetTimeEntryListFromTimeEntryDetailsCollection(ByVal recordset As List(Of TimeEntryDetails)) As List(Of TimeEntry)
            Dim entries As New List(Of TimeEntry)
            Dim record As TimeEntryDetails = Nothing
            For Each record In recordset
                entries.Add(GetTimeEntryFromTimeEntryDetails(record))
            Next
            Return entries
        End Function

        ''' <summary>
        ''' Helper Function to create a TimeEntry from TimeEntryDetails
        ''' </summary>
        Private Shared Function GetTimeEntryFromTimeEntryDetails(ByVal record As TimeEntryDetails) As TimeEntry
            Return New TimeEntry(record.time_entry_id, record.person_id, record.todo_item_id, record.project_id, record.entry_date, record.description, record.hours)
        End Function

#End Region

#Region "Properties"

        Private _time_entry_id As String
        Public Property time_entry_id() As String
            Get
                Return _time_entry_id
            End Get
            Set(ByVal value As String)
                _time_entry_id = value
            End Set
        End Property

        Private _person_id As Integer
        Public Property person_id() As Integer
            Get
                Return _person_id
            End Get
            Set(ByVal value As Integer)
                _person_id = value
            End Set
        End Property

        Private _todo_item_id As Integer
        Public Property todo_item_id() As Integer
            Get
                Return _todo_item_id
            End Get
            Set(ByVal value As Integer)
                _todo_item_id = value
            End Set
        End Property

        Private _project_id As Integer
        Public Property project_id() As Integer
            Get
                Return _project_id
            End Get
            Set(ByVal value As Integer)
                _project_id = value
            End Set
        End Property



        Private _entry_date As DateTime
        Public Property entry_date() As DateTime
            Get
                Return _entry_date
            End Get
            Set(ByVal value As DateTime)
                _entry_date = value
            End Set
        End Property

        Private _description As String
        Public Property description() As String
            Get
                Return _description
            End Get
            Set(ByVal value As String)
                _description = value
            End Set
        End Property

        Private _hours As Double
        Public Property hours() As Double
            Get
                Return _hours
            End Get
            Set(ByVal value As Double)
                _hours = value
            End Set
        End Property





#End Region

    End Class

End Namespace
